#coding=utf-8
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.db import models
from DjangoBbs.BbsSetting.models import  BbsClassType, BbsThemeType
from DjangoBbs.Help.models import  VisitPermClass, VisitPermTheme
from DjangoUeditor.models import UEditorField
from PublicTool.BaseModel import Base_UserModel

#from DjangoBbs.BbsTheme.models.BbsTheme

__author__ = 'czq'
#查询时不检索的地方
class BbsClassSearchManage(models.Manager):
    def get_query_set(self):
        from django.db.models import  Q
        return super(BbsClassSearchManage, self).get_query_set().filter(isHidden=False,visitPerm=None).filter(Q(typeId__code='bbs')|Q(typeId__code='travel'))

class BbsClass(Base_UserModel):
    name = models.CharField(u"板块名称",max_length=30)

    typeId = models.ForeignKey(BbsClassType,verbose_name=u"类型")
    isHidden= models.BooleanField(u"是否隐藏板块",default=False)

    classWapAgo=UEditorField(u'wap板块前',height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWapAfter=UEditorField(u"wap板块后",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWapTop=UEditorField(u"wap板块顶部",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWapDown=UEditorField(u"wap板块底部",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWebAgo=UEditorField(u'Web板块前',height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWebAfter=UEditorField(u"Web板块后",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWebTop=UEditorField(u"Web板块顶部",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)
    classWebDown=UEditorField(u"Web板块底部",height=200,width=600,imagePath="BbsClass",imageManagerPath="BbsClass",filePath="BbsClass",blank=True,null=True)

    #classRadio=models.CharField(max_length=50,blank=True,null=True)
    #adminUsers=models.ManyToManyField(u'管理员',max_length=100,blank=True,null=True)
    adminUsers=models.ManyToManyField(User,verbose_name=u'管理员',blank=True,null=True,related_name='adminUsers')
    #password=models.CharField(u"密码访问",max_length=20,blank=True,null=True)

    #权限
    visitPerm=models.ForeignKey(VisitPermClass,verbose_name=u"访问权限",blank=True,null=True)
    isAllowAddTheme=models.BooleanField(u"允许发表主题",default=True,help_text=u'是否可以在本版发表主题')
    isAllowAddReply=models.BooleanField(u"允许发表回复",default=True,help_text=u'是否可以在本版的主题进行回复')

    rank=models.IntegerField(u"排序",blank=True,null=True)
    parent=models.ForeignKey('self',verbose_name=u"上级",null=True,blank=True)

    themeCount=models.IntegerField(u'主题数量',default=0)
    replyCount=models.IntegerField(u'回复数量',default=0)
    #lastTheme=models.IntegerField(u'最新主题',default=0)
    #lastReply=models.IntegerField(u'最新回复',default=0)

    lastTheme=models.ForeignKey('BbsTheme.BbsTheme',verbose_name=u'最新主题',null=True,blank=True)
    lastReply=models.ForeignKey('BbsTheme.BbsReply',verbose_name=u'最新回复',null=True,blank=True)

    objects=models.Manager()
    objects_search=BbsClassSearchManage()

    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = '板块'
        verbose_name_plural = '板块'
        ordering = ['-addTime']

class ThemeStatus(models.Model):
    isTop=models.BooleanField(u"是否置顶",default=False)
    isDel=models.BooleanField(u'是否删除',default=False)
    class Meta:
        abstract = True

#查询没删除的
class BbsThemeManage(models.Manager):
    def get_query_set(self):
        return super(BbsThemeManage, self).get_query_set().filter(isDel=False)

#class BbsThemeBase(Base_UserModel,ThemeStatus):
#    pass
class BbsTheme(Base_UserModel,ThemeStatus):
    title=models.CharField(u"标题",max_length=40)
    content=UEditorField(u'内容',height=200,width=600,imagePath="BbsTheme",imageManagerPath="BbsTheme",filePath="BbsTheme",blank=True,null=True)
    classId=models.ForeignKey(BbsClass,verbose_name=u"板块")
    visitPerm=models.ForeignKey(VisitPermTheme,verbose_name=u"访问权限",blank=True,null=True)
    visitCount=models.IntegerField(u"浏览数量",default=0)
    replyCount=models.IntegerField(u"回复数量",default=0)
    themeType=models.ForeignKey(BbsThemeType,verbose_name=u'分类',null=True,blank=True)

    isLock=models.BooleanField(u'锁定',default=False)
    isBoutique=models.BooleanField(u"精华",default=False)
    isHighlight=models.BooleanField(u"高亮",default=False)
    isClose=models.BooleanField(u"结贴",default=False)
    isVote=models.BooleanField(u"投票",default=False)
    isSink=models.PositiveSmallIntegerField(u"提沉",default=0)

    lastReply=models.ForeignKey('BbsTheme.BbsReply',verbose_name=u'最新回复',null=True,blank=True)

    objects=models.Manager()
    objects_show=BbsThemeManage()

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '主题'
        verbose_name_plural = '主题'
        ordering = ['-addTime']



class BbsReply(Base_UserModel,ThemeStatus):
    theme=models.ForeignKey(BbsTheme)
    content=UEditorField(u'内容',height=200,width=600,imagePath="BbsReply",imageManagerPath="BbsReply",filePath="BbsReply",blank=True,null=True)
    classId=models.ForeignKey(BbsClass,blank=True,null=True)
    getMoney=models.IntegerField(u'获得金钱',blank=True,null=True)

    objects=models.Manager()
    objects_show=BbsThemeManage()

    def __unicode__(self):
        return self.content
    class Meta:
        verbose_name = '回复'
        verbose_name_plural = '回复'
        ordering = ['-addTime']

#import models_vote